package com.mecol.library.web;

import com.mecol.library.entity.*;
import com.mecol.library.service.BookClassService;
import com.mecol.library.service.BookService;
import com.mecol.library.service.LendService;
import com.mecol.library.service.UserService;
import com.mecol.library.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/lend")
public class LendController {

    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    @Autowired
    private LendService lendService;
    @Autowired
    private BookClassService  bookClassService;

    @RequestMapping("/lendList")
    public String userList(HttpSession session)
    {

        List<Book> books=bookService.getAllBooks();
        session.setAttribute("books",books);
        List<User> users=userService.getAllUsers();
        session.setAttribute("users",users);

        return "/jsp/lend/lendList";
    }



    @RequestMapping("/myLendList")
    public String myLendList(){

        return "/jsp/lend/myLendList";
    }


    @RequestMapping("/getMyLendList")
    @ResponseBody
    public ResultUtil getMyLendList(Integer page, Integer limit,HttpSession session)
    {
        Long user_id=  Long.valueOf(session.getAttribute("hotel_user_id").toString());
        return lendService.getMyLendList(page,limit,user_id);
    }


    @RequestMapping("/getAllLendList")
    @ResponseBody
    public ResultUtil getAllBookList(Integer page, Integer limit, LendSearch search)
    {
        return lendService.getAllLendList(page,limit,search);
    }



    @RequestMapping("/editLend/{ser_num}")
    public String editLend(@PathVariable("ser_num")int ser_num, HttpSession session)
    {

        Lend lend=lendService.getLendById(ser_num);

        session.setAttribute("lend",lend);
        List<Book> books=bookService.getAllBooks();
        session.setAttribute("books",books);
        List<User> users=userService.getAllUsers();
        session.setAttribute("users",users);

        return "/jsp/lend/editLend";
    }

    @RequestMapping("/updateLend")
    @ResponseBody
    public ResultUtil updateLend(Lend lend)throws ParseException
    {
        //System.out.println("曹操。。。。。。");

         lendService.updateLend(lend);
         return ResultUtil.ok();
    }


    @RequestMapping("/deleteLendById")
    @ResponseBody
    public ResultUtil deleteLendById(int ser_num)
    {
        lendService.deleteLendById(ser_num);
        return ResultUtil.ok();
    }


    @RequestMapping("/lendBook")
    public String lendBook(HttpSession session)
    {

        List<BookClass> bookClasses=bookClassService.getAllBookClasses();
        session.setAttribute("classes",bookClasses);
        return "/jsp/lend/lendBook";
    }


    @RequestMapping("backBookById")
    @ResponseBody
    public ResultUtil backBookById(int ser_num) throws ParseException {
        Lend lend=lendService.getLendById(ser_num);
        if(lend.getBack_date()==null) { //还没有归还
            Book book = bookService.getBookById((int) lend.getBook_id());
            book.setNumber(String.valueOf(Integer.valueOf(book.getNumber()) + 1));
            bookService.updateBook(book);


            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String nowTime = simpleDateFormat.format(date);
            Date back_date = simpleDateFormat.parse(nowTime);

            lend.setBack_date(back_date);

            lendService.updateLend(lend);
            return ResultUtil.ok();
        }
        else
        {
            return ResultUtil.error();
        }



    }



    @RequestMapping("/lendBookById")
    @ResponseBody
    public ResultUtil lendBookById(int book_id,HttpSession session) throws ParseException {
     //   lendService.deleteLendById(ser_num);
        //先判断数量是不是0 是领就返回错误信息 数量为0
        //然后 书本数量减1
        // session.setAttribute("hotel_user_id",user.getUser_id());中取出user_id 需要user_id 写入tb_lend_list表 需要

        Long user_id=  Long.valueOf(session.getAttribute("hotel_user_id").toString());

        Book book=bookService.getBookById(book_id);
        if(Integer.valueOf(book.getNumber())==0){ //数量为0 借阅失败

            return ResultUtil.error();
        }
        else {
            Integer num=Integer.valueOf(book.getNumber())-1;
            book.setNumber(String.valueOf(num));
            bookService.updateBook(book);

            Date date=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String nowTime=simpleDateFormat.format(date);
            Date lend_date=simpleDateFormat.parse(nowTime);

            lendService.insertNewLend((long) book_id,user_id,lend_date);

            return ResultUtil.ok();
        }

    }

}
